/**
*
* @file FIT轻运动
* @author Toma
* @time 2016年5月8日17:08:34
*
*/

/* 图片加载 */
var imgload = function () {
  $('.loadimg').each(function(i,el){
    var image = new Image();
    image.src = $(el).data('src');
    image.addEventListener('load',function(){
      $(el).css({'background-image':'url('+image.src+')'});
    });
  });
}();

/* 模拟链接 */
var link = function () {
  $('.href').on('tap',function(){
    window.location.href = $(this).data('href');
  });
}();


/**
 * @model 页面功能
 *
 */

/* 输入时禁止滚动 */
var oven = function(id){
  $(id).on('focus',function(){
    $('html,body').addClass('oven');
  });
  $(id).on('blur',function(){
    $('html,body').removeClass('oven');
  });
};

/* @from teacher_detail 弹出输入框 */
var showAdv = {
  event:function(){
    oven('.adv-txt textarea');
    $('#advshow').on('tap',showAdv.showAdv);
    $('.close, .adv-mask').on('tap',showAdv.hideAdv);
  },
  showAdv: function () {
    $('.advisory').show();
  },
  hideAdv: function () {
    $('.advisory').hide();
  },
  advAjax: function (url,data) {
    var loading;
    $('.adv-sub').on('tap',function(){
      data.data = $.trim($('.adv-txt textarea').val()); // 发送内容
      if(data.data){
        $.ajax({
          url: url,
          type: 'get',
          data: data,
          async: false,
          beforeSend: function(){
            loading = layer.open({type: 2});
          },
          success: function(){
            layer.close(loading);
            showAdv.lalert('<p class="ui-icon-emo" style="font-size: 90px;margin-bottom: 18px;line-height: 1;"></p><span>已收到您咨询的问题<br/>请关注个人中心交互信息</span>');
            $('.adv-txt textarea').val('');
          },
          error: function(){
            layer.close(loading);
            showAdv.lalert('没联网~');
          }
        });
        showAdv.hideAdv();
      }else{
        showAdv.lalert('请输入内容');
      }
    });
  },
  lalert: function (txt) {
    layer.open({
      content: txt,
      style: 'background-color:#fff; border:none;',
      time: 3
    });
  },
  init:function(){
    this.event();
  }
};


/* 启动时清算点赞数和评论数 */
var loadNmber = function(){
  var comNum = $('.fit-ul-list li').length,
    $com = $('#comNum'),
    ctext = $com.html().replace(/\d+/,comNum);
  $com.html(ctext);
  var zanNum = $('.head-waper .ui-avatar-tiled').length,
    $zan = $('#zanNum'),
    ztext = $zan.html().replace(/\d+/,zanNum);
  $zan.html(ztext);
};

/* 点赞 收藏 */
var Zan = function (id,type,url,data) {
  this.id = id;
  this.type = type;
  this.url = url;
  this.data = data;
  this.alg = true;
};
Zan.prototype = {
  init: function () {
    var _this = this;
    $(this.id).on('tap',function(){
      _this.alg = $(_this.id).hasClass('font-blue')? false:true;
      _this.zAjax(_this);
    });
  },
  zAjax: function (that) {
    var loading;
    that.data.alg = that.alg; // ture 增加
    $.ajax({
      url: that.url,
      type: 'get',
      data: that.data,
      async: false,
      beforeSend: function(){
        loading = layer.open({type: 2});
      },
      success: function(){
        layer.close(loading);
        that.callback();
      },
      error: function(){
        layer.close(loading);
        layer.open({
          content: '网络错误',
          time: 2
        });
      }
    });
  },
  callback: function () {
    switch(this.type){
      case 'zan':
        if(this.alg){
          $(this.id).addClass('font-blue');
          this.zalert('点赞成功');
          this.userHead(true);
        }else{
          $(this.id).removeClass('font-blue');
          this.zalert('取消点赞');
          this.userHead(false);
        }
        break;
      case 'coll':
        if(this.alg){
          $(this.id).addClass('font-blue');
          this.zalert('收藏成功');
          $(this.id).removeClass('icon-startlog').addClass('icon-satart');
        }else{
          $(this.id).removeClass('font-blue');
          this.zalert('取消收藏');
          $(this.id).removeClass('icon-satart').addClass('icon-startlog');
        }
        break;
    }
  },
  zalert: function (txt) {
    $.tips({
      content: txt,
      stayTime:2000,
      type:"success"
    });
  },
  userHead: function (bl) {
    if($('.head-waper').length > 0){
      if(bl){
        $('.head-waper').append('<span class="ui-avatar-tiled" data-id="'+this.data.userId+'"><span style="background-image:url('+this.data.userHead+')"></span></span>');
      }else{
        $('.head-waper span[data-id="'+this.data.userId+'"]').remove();
      }
      loadNmber();
    }
  }
};


/* 评论 */
var comment = {
  init: function (url,data) {
    oven('#comment');
    $('#commentBtn').on('tap', function(){
      data.data = $.trim($('#comment').val()); //评论内容
      var loading;
      if(data.data){
        $.ajax({
          url: url,
          type: 'get',
          data: data,
          async: false,
          beforeSend: function(){
            loading = layer.open({type: 2});
          },
          success: function(){
            layer.close(loading);
            comment.callback(data);
          },
          error: function(){
            layer.close(loading);
            layer.open({
              content: '网络错误',
              time: 2
            });
          }
        });
      }else{
        layer.open({
          content: '评论不能为空',
          time: 2
        });
      }
    });
  },
  callback: function (data) {
    var html = '<li class="ui-border-t">' +
                 '<div class="ui-avatar">' +
                   '<span style="background-image:url('+data.userHead+')"></span>' +
                 '</div>' +
                 '<div class="ui-list-info">' +
                   '<h4 class="ui-nowrap">'+ data.userName +
                    '<time class="log-time">刚才</time></h4>' +
                   '<p class="">'+data.data+'</p>' +
                 '</div>' +
               '</li>';
    $('.fit-ul-list').append(html);
    $('#comment').val('');
    loadNmber();
    $(window).scrollTop($(document).height());
  }
};


/* 发送手机验证码 */
var phoneCase = {
  url:'',
  data:{},
  event: function () {
    $('#veribtn').on('tap',phoneCase.verPhone);
  },
  verPhone: function () {
    var tel = $.trim($('#phone').val());
    if(/^1[3-8][0-9]\d{8}$/.test(tel)){
      phoneCase.eAjax(tel);
      phoneCase.time();
    }else{
      layer.open({
        content: '请输入正确的手机号',
        time: 2
      });
    }
  },
  time: function () {
    var $ve = $('#veribtn');
        $ve.off('tap');
    var speed = 10;
    var go = setInterval(function () {
      $ve.html(speed);
      if(speed == 0){
        clearInterval(go);
        $ve.html('重新发送');
        $ve.on('tap',phoneCase.verPhone);
      }
      speed--;
    },1000);
  },
  eAjax: function (tel) {
    var loading;
    this.data.tel = tel;  // 手机号码
    $.ajax({
      url: this.url,
      type: 'get',
      data: this.data,
      async: false,
      beforeSend: function(){
        loading = layer.open({type: 2});
      },
      success: function(){
        layer.close(loading);
        phoneCase.callback();
      },
      error: function(){
        layer.close(loading);
        layer.open({
          content: '网络错误',
          time: 2
        });
      }
    });
  },
  callback: function () {
    $.tips({
      content: '发送成功',
      stayTime:2000,
      type:"success"
    });
  },
  init: function (url,data) {
    this.event();
    this.url = url;
    this.data = data;
  }
};

/* 提交表单 */
var formCase = {
  reset: function () {
    var date = new Date(),
        year = date.getFullYear(),
        yearTag = '',
        monthTag = '';
    for(var i = 1; i<=12; i++){
      if(i<10){
        i = '0' + i;
      }
      monthTag+='<option value="'+i+'">'+i+'</option>';
    }
    for(var j = 0; j<= 100; j++){
      var vg = year - j;
      yearTag+='<option value="'+vg+'">'+vg+'</option>';
    }
    var dmon = $('#setMon').data('mon'),
        dyear = $('#setYear').data('year');
    $('#setMon').html(monthTag);
    $('#setMon option[value="'+ dmon +'"]').prop('selected',true);
    $('#setYear').html(yearTag);
    $('#setYear option[value="'+ dyear +'"]').prop('selected',true);
  },
  init: function () {
    this.reset();
  }
};


/* 查看信息 */
var getMessage = {
  url:'',
  data:{},
  event: function () {
    $('.fit-ul-list li').on('tap',getMessage.showMessage);
  },
  showMessage: function () {
    $(".ui-dialog").dialog("show");
    $('.ui-dialog-cnt').width($(window).width()-40)
      .find('.fit-d-com').height($(window).height() *.5);
    $('.fit-d-name').html($.trim($(this).find('.ui-list-info h4').html()));
    $('.fit-d-com').html($.trim($(this).find('.ui-list-info p').html()));

    getMessage.gAjax(this,$(this).data('id'));
  },
  gAjax: function (that, id) {
    this.data.id = id; // 信息ID
    $.ajax({
      url: this.url,
      type: 'get',
      data: this.data,
      async: false,
      success: function(){
        $(that).addClass('readed').find('.fit-info-l i').removeClass('icon-email').addClass('icon-emailopen');
      }
    });
  },
  init: function (url, data) {
    this.url = url;
    this.data = data;
    this.event();
  }
};

/* 移除收藏 */
var delCollect = {
  url:'',
  data:{},
  event: function () {
    $('.coll-del li .ui-icon-delete').on('tap', function (e) {
      e.stopPropagation();
      e.preventDefault();
      delCollect.dAjax(this);
    });
  },
  dAjax: function (that) {
    var par = $(that).parents('.href')[0];
    this.data.id = $(par).data('id');  // 视频ID
    $.ajax({
      url: this.url,
      type: 'get',
      data: this.data,
      async: false,
      success: function(){
        par.remove();
        $.tips({
          content: '移除收藏',
          stayTime:2000,
          type:"success"
        });
      },
      error: function(){
        $.tips({
          content: '网络错误',
          stayTime:2000,
          type:"warn"
        });
      }
    });
  },
  init: function (url, data) {
    this.url = url;
    this.data = data;
    this.event();
  }
};


/* 聊天 交互 */
var mutual = {
  url:'',
  data:{},
  reset: function () {
    $('.ui-list-info span').each(function(){
      if($(this).height() > 45){
        $(this).addClass('gatrow');
      }
    });
    $(window).scrollTop($(document).height());
    $('.an-up').removeClass('an-up');
  },
  event: function () {
    $('#mutualBtn').on('tap', function (e) {
      e.stopPropagation();
      e.preventDefault();
      var data = $.trim($('#mutual').val());
      if(data){
        mutual.mAjax(data);
      }
    });
  },
  mAjax: function (data) {
    this.data.data = data; // 内容
    $.ajax({
      url: this.url,
      type: 'get',
      data: this.data,
      async: false,
      success: function(){
        var html = '<li class="li-me an-up">' +
                    '<div class="ui-list-info"><span>'+ mutual.data.data +'</span></div>' +
                    '<div class="ui-avatar-s">' +
                      '<span style="background-image:url('+ mutual.data.headimg +')"></span>' +
                    '</div>' +
                   '</li>';
        $('.mu-ul').append(html);
        $('#mutual').val('');
        mutual.reset();
      },
      error: function(){
        $.tips({
          content: '网络错误',
          stayTime:2000,
          type:"warn"
        });
      }
    });
  },
  init: function (url, data) {
    this.url = url;
    this.data = data;
    this.event();
    this.reset();
  }
};


/* 分享 */
var share = {
  show:function(){
    $('.share').show();
  },
  hide:function(){
    $('.share').hide();
  },
  event:function(){
    $('#share').on('tap',share.show);
    $('.share').on('tap',share.hide);
  }
};